clear all
close all
clc

pref=fopen('image_ref.img');
pseuils=fopen('img_sueils.img');

dim = fscanf(pref, '%u,',2); % [Width height]
dim = fscanf(pseuils, '%u,',2);

ref = fscanf(pref, '%u,',4*dim(1)*dim(2));
seuils = fscanf(pseuils, '%u,',2*dim(1)*dim(2));

idx=1:dim(1)*dim(2);

image_ref_B=ref(4*idx-3);
image_ref_G=ref(4*idx-2);
image_ref_R=ref(4*idx-1);
image_ref_B=reshape(image_ref_B,dim(1),dim(2));
image_ref_G=reshape(image_ref_G,dim(1),dim(2));
image_ref_R=reshape(image_ref_R,dim(1),dim(2));
image_ref(:,:,3)=uint8(image_ref_B);
image_ref(:,:,2)=uint8(image_ref_G);
image_ref(:,:,1)=uint8(image_ref_R);

image_seuils=reshape(seuils,dim(1),dim(2));
img_seuils_vert=mod(floor(image_seuils./2),2);
img_seuils_rouge=mod(floor(image_seuils./16),2);
fclose(pref);
fclose(pseuils);

perode=fopen('img_erode.img');
dim = fscanf(perode, '%u,',2); % [Width height]
ierode = fscanf(perode, '%u,',dim(1)*dim(2));
img_erode=reshape(ierode,dim(1),dim(2));
img_erode_vert=mod(floor(img_erode./2),2);
img_erode_rouge=mod(floor(img_erode./16),2);
fclose(perode);

plabels1=fopen('img_labels_s1_f.img');
dim = fscanf(plabels1, '%u,',2);
labels1 = fscanf(plabels1, '%u,',dim(1)*dim(2));
image_labels1=reshape(labels1,dim(1),dim(2));
fclose(plabels1);

plabels2=fopen('img_labels_s4_f.img');
dim = fscanf(plabels2, '%u,',2);
labels2 = fscanf(plabels2, '%u,',dim(1)*dim(2));
image_labels2=reshape(labels2,dim(1),dim(2));
fclose(plabels2);

plabels1i=fopen('img_labels_s1_i.img');
dim = fscanf(plabels1i, '%u,',2);
labels1i = fscanf(plabels1i, '%u,',dim(1)*dim(2));
image_labels1i=reshape(labels1i,dim(1),dim(2));
fclose(plabels1i);

plabels2i=fopen('img_labels_s4_i.img');
dim = fscanf(plabels2i, '%u,',2);
labels2i = fscanf(plabels2i, '%u,',dim(1)*dim(2));
image_labels2i=reshape(labels2i,dim(1),dim(2));
fclose(plabels2i);


figure(1)
image(image_ref);

figure(2)
subplot 241
imagesc(img_seuils_vert);
title('Seuils vert');
subplot 242
imagesc(img_erode_vert);
title('Erosion vert');
subplot 243
imagesc(image_labels1i);
title('Label vert initial');
subplot 244
imagesc(image_labels1);
title('Label vert');

subplot 245
imagesc(img_seuils_rouge);
title('Seuils rouge');
subplot 246
imagesc(img_erode_rouge);
title('Erosion rouge');
subplot 247
imagesc(image_labels2i);
title('Label rouge initial');
subplot 248
imagesc(image_labels2);
title('Label rouge');



